package ldh.im.fxbase.rpc.router;

import ldh.im.dto.base.OkResp;
import ldh.im.dto.business.PingReq;
import ldh.im.dto.business.webrtc.WebRtcDelRoomReq;
import ldh.im.dto.business.webrtc.WebRtcExitRoomReq;
import ldh.im.dto.business.webrtc.WebRtcJoinRoomReq;
import ldh.im.dto.business.webrtc.mesh.WebRtcP2pCandidateReq;
import ldh.im.dto.business.webrtc.mesh.WebRtcP2pSdpReq;
import ldh.im.fxbase.rpc.SignalCallBack;
import ldh.rpc.util.JsonUtil;
import ldh.rpc.Rpc;
import ldh.rpc.RpcMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * WebRtc信令服务接受服务端发送的消息
 */
@Rpc
public class SignalConsumerRouter {

    private static final Logger LOGGER = LoggerFactory.getLogger(SignalConsumerRouter.class);

    private List<SignalCallBack> signalCallBackList = new ArrayList();

    public void addSignalCallBack(SignalCallBack callBack) {
        signalCallBackList.add(callBack);
    }

    /**
     * 接受服务端的ping请求
     * @param pingReq
     * @return
     */
    @RpcMapper(path="pingToClient", asynHandle = true)
    public OkResp pingToClient(PingReq pingReq) {
        LOGGER.info("[signal receive ping]");
        for (SignalCallBack callBack : signalCallBackList) {
            callBack.pingToClient(pingReq);
        }
        return new OkResp(pingReq.getLocalSeq());
    }

    /**
     * 接受服务器端转发的进入房间请求
     * @param joinRoomReq
     * @return
     */
    @RpcMapper(path="serverSendRoomJoin", asynHandle = true)
    public OkResp receiveJoinRoomReq(WebRtcJoinRoomReq joinRoomReq) {
        LOGGER.info("[signal receive join room req], data: {}", JsonUtil.toJson(joinRoomReq));
        OkResp okResp = null;
        for (SignalCallBack callBack : signalCallBackList) {
            okResp = callBack.receiveJoinRoomReq(joinRoomReq);
        }
        return okResp == null ? new OkResp(joinRoomReq.getLocalSeq()) : okResp;
    }

    /**
     * 接受服务端转发的退出房间请求
     * @param exitRoomReq
     * @return
     */
    @RpcMapper(path="serverSendRoomExit", asynHandle = true)
    public OkResp receiveExitRoomReq(WebRtcExitRoomReq exitRoomReq) {
        LOGGER.info("[signal receive exit room req], data: {}", JsonUtil.toJson(exitRoomReq));
        OkResp okResp = null;
        for (SignalCallBack callBack : signalCallBackList) {
            okResp = callBack.receiveExitRoomReq(exitRoomReq);
        }
        return okResp == null ? new OkResp(exitRoomReq.getLocalSeq()) : okResp;
    }

    /**
     * 接受服务端煮饭的Sdp请求
     * @param sdpReq
     * @return
     */
    @RpcMapper(path="serverSendSdp", asynHandle = true)
    public OkResp receiveSdp(WebRtcP2pSdpReq sdpReq) {
        LOGGER.info("[signal receive sdp req], data: {}", JsonUtil.toJson(sdpReq));
        OkResp okResp = null;
        for (SignalCallBack callBack : signalCallBackList) {
            okResp = callBack.receiveSdp(sdpReq);
        }
        return okResp == null ? new OkResp(sdpReq.getLocalSeq()) : okResp;
    }

    /**
     * 接受服务端转发Candidate请求
     * @param webRtcP2pCandidateReq
     * @return
     */
    @RpcMapper(path="serverSendCandidate", asynHandle = true)
    public OkResp receiveIceCandidateReq(WebRtcP2pCandidateReq webRtcP2pCandidateReq) {
        LOGGER.info("[signal receive iceCandidate req], data: {}", JsonUtil.toJson(webRtcP2pCandidateReq));
        OkResp okResp = null;
        for (SignalCallBack callBack : signalCallBackList) {
            okResp = callBack.receiveIceCandidateReq(webRtcP2pCandidateReq);
        }
        return okResp == null ? new OkResp(webRtcP2pCandidateReq.getLocalSeq()) : okResp;
    }

    /**
     * 接受服务器的删除房间请求
     * @param delRoomReq
     * @return
     */
    @RpcMapper(path="serverSendRoomDel", asynHandle = true)
    public OkResp receiveDelRoomReq(WebRtcDelRoomReq delRoomReq) {
        LOGGER.info("[signal receive del room req], data: {}", JsonUtil.toJson(delRoomReq));
        OkResp okResp = null;
        for (SignalCallBack callBack : signalCallBackList) {
            okResp = callBack.receiveDelRoomReq(delRoomReq);
        }
        return okResp == null ? new OkResp(delRoomReq.getLocalSeq()) : okResp;
    }
}
